package com.qike.duola.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.qike.duola.dto.LocationEntity;
import com.qike.duola.dto.ReceiveCallDto;
import com.qike.duola.enums.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.BaseService;
import com.qike.duola.utils.voice.VoiceCallMain;
import com.qike.duola.utils.voice.VoiceNotify;
import com.qike.duola.utils.voice.VoiceNotifyMain;
import com.qike.duola.vo.EnumList;
import com.qike.duola.vo.FixInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.qike.duola.utils.voice.VoiceNotifyMain.callNotifyAPI;

@RestController
@Api(description = "基础控制器")
@CrossOrigin
public class BaseController {
    private static Logger logger = LoggerFactory.getLogger(BaseController.class);


    @Autowired
    private BaseService baseService;

    // 接口返回值
    private static String status = "";
    private static String resultcode = "";
    private static String resultdesc = "";
    // 语音通知接口返回值
    private static String sessionId = "";
    // 语音通知业务类实体
    public static VoiceNotify callNotifyAPI = new VoiceNotify();

    // 调用接口成功标识
    private static final String success = "200";


    /**
     * 上传文件
     *
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/upLoadPhoto")
    @ResponseBody
//    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult upLoadPhoto(@RequestBody MultipartFile file) {
        try {
            String url = baseService.upLoadPhoto(file);
            return RetResponse.makeOKRsp(url);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 客户评级列表
     *
     * @return
     */
    @ApiOperation("客户评级列表")
    @GetMapping("/customerLevel")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult customerLevel() {
        try {
            List<EnumList> enumList = CustomerLevelEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 搅拌站信息
     *
     * @return
     */
    @ApiOperation("搅拌站信息")
    @GetMapping("/fixInfo")
    @ResponseBody
    public BaseResult fixInfo() {
        try {
            FixInfoVo vo = baseService.fixInfo();
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 结算方式列表
     *
     * @return
     */
    @ApiOperation("结算方式列表")
    @GetMapping("/payType")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult payType() {
        try {
            List<EnumList> enumList = PayTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }




    /**
     * 是否挂靠列表
     *
     * @return
     */
    @ApiOperation("是否挂靠列表")
    @GetMapping("/isOut")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult isOut() {
        try {
            List<EnumList> enumList = IsOutEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 车辆运输状态列表
     *
     * @return
     */
    @ApiOperation("车辆运输状态列表")
    @GetMapping("/CarTransStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult CarTransStatusEnums() {
        try {
            List<EnumList> enumList = CarTransStatusEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 车辆状态列表
     *
     * @return
     */
    @ApiOperation("车辆状态列表")
    @GetMapping("/CarStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult CarStatusEnums() {
        try {
            List<EnumList> enumList = IsOutEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 呼叫电话
     *
     * @return
     */
    @ApiOperation("呼叫电话 http://60.205.213.18:8092/receiveCall")
    @PostMapping("/receiveCall")
    @ResponseBody
    public BaseResult receiveCall(@RequestBody ReceiveCallDto dto  ) {
        try {
            System.out.println(JSON.toJSON(dto));
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 车辆类型列表
     *
     * @return
     */
    @ApiOperation("车辆类型列表")
    @GetMapping("/CarTypeEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult CarTypeEnums() {
        try {
            List<EnumList> enumList = CarTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 企业类型列表
     *
     * @return
     */
    @ApiOperation("企业类型列表")
    @GetMapping("/CompanyTypeEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult CompanyTypeEnums() {
        try {
            List<EnumList> enumList = CompanyTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 合同状态列表
     *
     * @return
     */
    @ApiOperation("合同状态列表")
    @GetMapping("/ContractStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult ContractStatusEnums() {
        try {
            List<EnumList> enumList = ContractStatusEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 合同类型列表
     *
     * @return
     */
    @ApiOperation("合同类型列表")
    @GetMapping("/ContractTypeEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult ContractTypeEnums() {
        try {
            List<EnumList> enumList = ContractTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 添加合同类型列表
     *
     * @return
     */
    @ApiOperation("添加合同类型列表")
    @GetMapping("/AddContractTypeEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult AddContractTypeEnums() {
        try {
            List<EnumList> enumList = AddContractTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 客户状态
     *
     * @return
     */
    @ApiOperation("客户状态列表")
    @GetMapping("/CustomerStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult CustomerStatusEnums() {
        try {
            List<EnumList> enumList = CustomerStatusEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 审批状态枚举
     *
     * @return
     */
    @ApiOperation("审批状态枚举")
    @GetMapping("/ExamineEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult ExamineEnums() {
        try {
            List<EnumList> enumList = ExamineEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 商品类型枚举
     *
     * @return
     */
    @ApiOperation("商品类型枚举")
    @GetMapping("/GoodsTypeEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult GoodsTypeEnums() {
        try {
            List<EnumList> enumList = GoodsTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 生产计划状态 0 未发货 1 发货中 2 发货完成
     *
     * @return
     */
    @ApiOperation("生产计划状态")
    @GetMapping("/PlanOrderStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult PlanOrderStatusEnums() {
        try {
            List<EnumList> enumList = PlanOrderStatusEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 运输单枚举
     *
     * @return
     */
    @ApiOperation("运输单状态 1 待装料 2 运输中 3 装料完成 4 到达工地 5 浇筑中 6 回程中 7 退料回程中 8 运输完成")
    @GetMapping("/TransOrderStatusEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult TransOrderStatusEnums() {
        try {
            List<EnumList> enumList = TransOrderStatusEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 消息类型枚举
     *
     * @return
     */
    @ApiOperation("消息类型 0系统 1活动")
    @GetMapping("/messageEnums")
    @ResponseBody
    @Cacheable("enum")
    public BaseResult messageEnums() {
        try {
            List<EnumList> enumList = MessageTypeEnums.getEnumList();
            return RetResponse.makeOKRsp(enumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 运输单枚举
     *
     * @return
     */
    @ApiOperation("运输单状态 1 待装料 2 运输中 3 装料完成 4 到达工地 5 浇筑中 6 回程中 7 退料回程中 8 运输完成")
    @GetMapping("/testTime")
    @ResponseBody
    public BaseResult testTime() {
        try {
            LocalDateTime now = LocalDateTime.now();
            LocalDate now1 = LocalDate.now();
            LocalDateTime min = LocalDateTime.of(now1, LocalTime.MIN);
            LocalDateTime max = LocalDateTime.of(now1, LocalTime.MAX);
            return RetResponse.makeOKRsp(now+"----"+now1+"______"+min+"__________"+max);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 测试模拟车辆运行
     *
     * @return
     */
    @ApiOperation("测试模拟车辆运行")
    @PostMapping("/testCarRun")
    @ResponseBody
    public BaseResult testCarRun(@RequestBody LocationEntity locationEntity ) {
        try {
          baseService.testCarRun(locationEntity);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }




}
